package hs4m.kidseducational;

import hs4m.kidseducational.hear.HearActivity;
import hs4m.kidseducational.math.MathActivity;
import hs4m.kidseducational.math.MathLevelActivity;
import hs4m.kidseducational.memory.MemoryLevelActivity;
import hs4m.kidseducational.paint.PaintActivity;
import hs4m.kidseducational.sentence.SentenceActivity;
import hs4m.kidseducational.util.AnimationFactory;
import hs4m.kidseducational.util.SystemUiHider;
import hs4m.kidseducational.util.Utils;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.devsmart.android.ui.HorizontalListView;

/**
 * An example full-screen activity that shows and hides the system UI (i.e. status bar and navigation/system bar) with user interaction.
 * 
 * @see SystemUiHider
 */
public class HomepageActivity extends Activity {

	public Typeface typeFace;
	MediaPlayer mp;
	ImageView sound;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_homepage);		
		sound = (ImageView) findViewById(R.id.sound);
		if (Utils.getSound(getApplicationContext())) {
			sound.setBackgroundDrawable(getResources().getDrawable(R.drawable.icon_volume_on));
			playMusic();
		}
		typeFace = Typeface.createFromAsset(getAssets(), "fonts/VNFREE.TTF");

		HorizontalListView listview = (HorizontalListView) findViewById(R.id.listview);
		listview.setAdapter(mAdapter);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
				Intent intent;
				switch (position) {
				case 0:
					// Lam Toan
					intent = new Intent(getApplicationContext(), MathLevelActivity.class);
					startActivityForResult(intent, 1);
					break;
				case 1:
					// Dat Cau
					intent = new Intent(getApplicationContext(), SentenceActivity.class);
					startActivityForResult(intent, 1);
					break;
				case 2:
					// Ghi nho
					intent = new Intent(getApplicationContext(), MemoryLevelActivity.class); // MemoryActivity.class
					startActivityForResult(intent, 1);
					break;
				case 3:
					// Ve
					intent = new Intent(getApplicationContext(), PaintActivity.class);
					startActivityForResult(intent, 1);
					break;
				case 4:
					// Nghe
					intent = new Intent(getApplicationContext(), HearActivity.class);
					startActivityForResult(intent, 1);
					break;
				default:
					break;
				}
			}
		});

		LinearLayout category = (LinearLayout) findViewById(R.id.list_category);
		Animation in = AnimationFactory.inFromBottom();
		in.setDuration(1200);
		category.setAnimation(in);

		// // Gallery
		// myFastGallery = (Gallery) findViewById(R.id.fastgallery);
		//
		// // myGalleryBaseAdapter = new GalleryBaseAdapter(this);
		// myCacheGalleryBaseAdapter = new CacheGalleryBaseAdapter(this);
		//
		// String ExternalStorageDirectoryPath = Environment
		// .getExternalStorageDirectory().getAbsolutePath();
		//
		// String targetPath = ExternalStorageDirectoryPath + "/kidsedu";
		//
		// File targetDirector = new File(targetPath);
		//
		// File[] files = targetDirector.listFiles();
		// for (File file : files) {
		// // myGalleryBaseAdapter.add(file.getPath());
		// myCacheGalleryBaseAdapter.add(file.getPath());
		// }
		// myFastGallery.setAdapter(myCacheGalleryBaseAdapter);
		//
		// myFastGallery.setOnItemClickListener(new OnItemClickListener() {
		//
		// @Override
		// public void onItemClick(AdapterView<?> adap, View view, int pos,
		// long id) {
		// forwardToGame(pos);
		// }
		// });
		//
		// // Get memory class of this device, exceeding this amount will throw an
		// // OutOfMemory exception.
		// final int memClass = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE))
		// .getMemoryClass();
		//
		// // Use 1/8th of the available memory for this memory cache.
		// final int cacheSize = 1024 * 1024 * memClass / 8;
		//
		// mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
		//
		// protected int sizeOf(String key, Bitmap bitmap) {
		// // The cache size will be measured in bytes rather than number
		// // of items.
		// return bitmap.getByteCount();
		// }
		//
		// };
	}

	private static String[] dataObjects = new String[] { "Text #1", "Text #2", "Text #3", "Text #4", "Text #5" };

	private BaseAdapter mAdapter = new BaseAdapter() {

		// private OnClickListener mOnButtonClicked = new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// AlertDialog.Builder builder = new AlertDialog.Builder(HomepageActivity.this);
		// builder.setMessage("hello from " + v);
		// builder.setPositiveButton("Cool", null);
		// builder.show();
		//
		// }
		// };

		@Override
		public int getCount() {
			return dataObjects.length;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.viewitem, null);

			TextView title = (TextView) retval.findViewById(R.id.title);
			title.setTypeface(typeFace);
			// Button button = (Button) retval.findViewById(R.id.clickbutton);
			// button.setOnClickListener(mOnButtonClicked);
			// title.setText(dataObjects[position]);
			ImageButton image = (ImageButton) retval.findViewById(R.id.image);

			// Hieu ung cho image
			image.startAnimation(Utils.animSlideBottom2Top());
			// Het hieu ung

			switch (position) {
			case 0:
				// Lam Toan
				image.setImageResource(R.drawable.lamtoandep);
				title.setText(getResources().getString(R.string.title_activity_math));
				// image.setOnClickListener(new OnClickListener() {
				// @Override
				// public void onClick(View v) {
				// Intent intent = new Intent(getApplicationContext(), MathActivity.class);
				// startActivityForResult(intent, 1);
				// }
				// });
				break;
			case 1:
				// Dat Cau
				image.setImageResource(R.drawable.datcau);
				title.setText(getResources().getString(R.string.title_activity_sentence));
				// image.setOnClickListener(new OnClickListener() {
				// @Override
				// public void onClick(View v) {
				// Intent intent = new Intent(getApplicationContext(), SentenceActivity.class);
				// startActivityForResult(intent, 1);
				// }
				// });
				break;
			case 2:
				// Ghi nho
				image.setImageResource(R.drawable.luyentrinho);
				title.setText(getResources().getString(R.string.title_activity_memory));
				// image.setOnClickListener(new OnClickListener() {
				// @Override
				// public void onClick(View v) {
				// Intent intent = new Intent(getApplicationContext(), MemoryLevelActivity.class); //MemoryActivity.class
				// startActivityForResult(intent, 1);
				// }
				// });
				break;
			case 3:
				// Ve
				image.setImageResource(R.drawable.vetomau);
				title.setText(getResources().getString(R.string.title_activity_paint));
				// image.setOnClickListener(new OnClickListener() {
				// @Override
				// public void onClick(View v) {
				// Intent intent = new Intent(getApplicationContext(), PaintActivity.class);
				// startActivityForResult(intent, 1);
				// }
				// });
				break;
			case 4:
				// Nghe
				image.setImageResource(R.drawable.doanhinh);
				title.setText(getResources().getString(R.string.title_activity_hear));
				// image.setOnClickListener(new OnClickListener() {
				// @Override
				// public void onClick(View v) {
				// Intent intent = new Intent(getApplicationContext(), HearActivity.class);
				// startActivityForResult(intent, 1);
				// }
				// });
				break;
			default:
				break;
			}

			return retval;
		}

	};

	private void playMusic() {
		if (mp == null) {
			mp = MediaPlayer.create(this, R.raw.con_heo_dat);
			mp.setOnCompletionListener(new OnCompletionListener() {
				@Override
				public void onCompletion(MediaPlayer mp) {
					mp.release();
				}
			});
			mp.start();
			mp.setLooping(true);
			mp.setVolume(0.6f, 0.6f);
		}
	}

	public void turnVolumeOnclick(View view) {
		Utils.clickSound(this);
		if (Utils.getSound(getApplicationContext())) {
			sound.setBackgroundDrawable(getResources().getDrawable(R.drawable.icon_volume_mute));
			Utils.setSound(getApplicationContext(), false);
			if (mp != null) {
				mp.stop();
			}
			mp = null;
		} else {
			sound.setBackgroundDrawable(getResources().getDrawable(R.drawable.icon_volume_on));
			Utils.setSound(getApplicationContext(), true);
			playMusic();
		}
	}

	@Override
	protected void onDestroy() {
		if (mp != null) {
			mp.stop();
		}
		super.onDestroy();
	}

	// @Override
	// protected void onPostCreate(Bundle savedInstanceState) {
	// super.onPostCreate(savedInstanceState);
	// }

	// /**
	// * Touch listener to use for in-layout UI controls to delay hiding the
	// * system UI. This is to prevent the jarring behavior of controls going away
	// * while interacting with activity UI.
	// */
	// View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
	// @Override
	// public boolean onTouch(View view, MotionEvent motionEvent) {
	//
	// return false;
	// }
	// };
	//
	// // =============== Galery =================
	// // ADAPTER
	// public class GalleryBaseAdapter extends BaseAdapter {
	//
	// ArrayList<String> GalleryFileList;
	// Context context;
	//
	// GalleryBaseAdapter(Context cont) {
	// context = cont;
	// GalleryFileList = new ArrayList<String>();
	// }
	//
	// @Override
	// public int getCount() {
	// return GalleryFileList.size();
	// }
	//
	// @Override
	// public Object getItem(int position) {
	// return GalleryFileList.get(position);
	// }
	//
	// @Override
	// public long getItemId(int position) {
	// return position;
	// }
	//
	// @Override
	// public View getView(int position, View convertView, ViewGroup parent) {
	//
	// Bitmap bm = decodeSampledBitmapFromUri(
	// GalleryFileList.get(position), 200, 200);
	// LinearLayout layout = new LinearLayout(context);
	// layout.setLayoutParams(new Gallery.LayoutParams(250, 250));
	// layout.setGravity(Gravity.CENTER);
	//
	// ImageView imageView = new ImageView(context);
	// imageView.setLayoutParams(new Gallery.LayoutParams(200, 200));
	// imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
	// imageView.setImageBitmap(bm);
	//
	// layout.addView(imageView);
	// return layout;
	// }
	//
	// public void add(String newitem) {
	// GalleryFileList.add(newitem);
	// }
	//
	// }
	//
	// // ADAPTER CACHE
	// public class CacheGalleryBaseAdapter extends GalleryBaseAdapter {
	//
	// CacheGalleryBaseAdapter(Context cont) {
	// super(cont);
	// }
	//
	// @Override
	// public View getView(int position, View convertView, ViewGroup parent) {
	//
	// ImageView imageView = new ImageView(context);
	//
	// // ---
	// // Use the path as the key to LruCache
	// final String imageKey = GalleryFileList.get(position);
	//
	// final Bitmap bm = getBitmapFromMemCache(imageKey);
	// if (bm == null) {
	// BitmapWorkerTask task = new BitmapWorkerTask(imageView);
	// task.execute(imageKey);
	// }
	//
	// LinearLayout layout = new LinearLayout(context);
	// layout.setLayoutParams(new Gallery.LayoutParams(360, 360));
	// layout.setGravity(Gravity.CENTER);
	//
	// imageView.setLayoutParams(new Gallery.LayoutParams(300, 300));
	// imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
	// imageView.setImageBitmap(bm);
	//
	// layout.addView(imageView);
	// return layout;
	//
	// }
	//
	// }
	//
	// // GalleryBaseAdapter myGalleryBaseAdapter;
	// CacheGalleryBaseAdapter myCacheGalleryBaseAdapter;
	// Gallery myFastGallery;
	//
	// private LruCache<String, Bitmap> mMemoryCache;
	//
	// // AsyncTask
	// class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
	//
	// private final WeakReference<ImageView> imageViewReference;
	//
	// public BitmapWorkerTask(ImageView imageView) {
	// // Use a WeakReference to ensure the ImageView can be garbage
	// // collected
	// imageViewReference = new WeakReference<ImageView>(imageView);
	// }
	//
	// @Override
	// protected Bitmap doInBackground(String... params) {
	// final Bitmap bitmap = decodeSampledBitmapFromUri(params[0], 200,
	// 200);
	// addBitmapToMemoryCache(String.valueOf(params[0]), bitmap);
	// return bitmap;
	// }
	//
	// @Override
	// protected void onPostExecute(Bitmap bitmap) {
	// if (imageViewReference != null && bitmap != null) {
	// final ImageView imageView = (ImageView) imageViewReference
	// .get();
	// if (imageView != null) {
	// imageView.setImageBitmap(bitmap);
	// }
	// }
	// }
	//
	// }
	//
	// // ==========================
	//
	// public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
	// if (getBitmapFromMemCache(key) == null) {
	// if (key != null && bitmap != null) {
	// mMemoryCache.put(key, bitmap);
	// }
	// }
	// }
	//
	// public Bitmap getBitmapFromMemCache(String key) {
	// return (Bitmap) mMemoryCache.get(key);
	// }
	//
	// // ===========================
	// public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth,
	// int reqHeight) {
	// Bitmap bm = null;
	//
	// // First decode with inJustDecodeBounds=true to check dimensions
	// final BitmapFactory.Options options = new BitmapFactory.Options();
	// options.inJustDecodeBounds = true;
	// BitmapFactory.decodeFile(path, options);
	//
	// // Calculate inSampleSize
	// options.inSampleSize = calculateInSampleSize(options, reqWidth,
	// reqHeight);
	//
	// // Decode bitmap with inSampleSize set
	// options.inJustDecodeBounds = false;
	// bm = BitmapFactory.decodeFile(path, options);
	//
	// return bm;
	// }
	//
	// public int calculateInSampleSize(BitmapFactory.Options options,
	// int reqWidth, int reqHeight) {
	// // Raw height and width of image
	// final int height = options.outHeight;
	// final int width = options.outWidth;
	// int inSampleSize = 1;
	//
	// if (height > reqHeight || width > reqWidth) {
	// if (width > height) {
	// inSampleSize = Math.round((float) height / (float) reqHeight);
	// } else {
	// inSampleSize = Math.round((float) width / (float) reqWidth);
	// }
	// }
	//
	// return inSampleSize;
	// }
	//
	// private void forwardToGame(int idGame) {
	// switch (idGame) {
	// case 0:
	// // Math - Tro choi lam toan
	// Toast.makeText(getApplicationContext(), "Math", Toast.LENGTH_SHORT)
	// .show();
	// Intent intent = new Intent(getApplicationContext(),
	// MathActivity.class);
	// startActivityForResult(intent, 1);
	// break;
	// case 1:
	// // Sentence - Tro choi dat cau
	// Toast.makeText(getApplicationContext(), "Sentence",
	// Toast.LENGTH_SHORT).show();
	// Intent sen = new Intent(getApplicationContext(), SentenceActivity.class);
	// startActivityForResult(sen, 1);
	// break;
	// case 2:
	// // Memory - Tro choi ghi nho hinh anh
	// Toast.makeText(getApplicationContext(), "Memory",
	// Toast.LENGTH_SHORT).show();
	// Intent mem = new Intent(getApplicationContext(), MemoryActivity.class);
	// startActivityForResult(mem, 1);
	// break;
	// case 3:
	// // Paint - Ve va to mau tranh
	// Toast.makeText(getApplicationContext(), "Paint", Toast.LENGTH_SHORT)
	// .show();
	// Intent pai = new Intent(getApplicationContext(), PaintActivity.class);
	// startActivityForResult(pai, 1);
	// break;
	// case 4:
	// // Hear - Nghe va chon hinh tuong ung
	// Toast.makeText(getApplicationContext(), "Hear", Toast.LENGTH_SHORT)
	// .show();
	// Intent her = new Intent(getApplicationContext(), HearActivity.class);
	// startActivityForResult(her, 1);
	// break;
	//
	// default:
	// // Math
	// break;
	// }
	// }
}
